package com.enation.app.javashop.core.shop.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.enation.app.javashop.core.client.system.LogiCompanyClient;
import com.enation.app.javashop.framework.util.ReflectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.enation.app.javashop.core.shop.ShopErrorCode;
import com.enation.app.javashop.core.system.model.dos.LogiCompanyDO;
import com.enation.app.javashop.core.shop.service.ShopLogiCompanyManager;
import com.enation.app.javashop.framework.context.UserContext;
import com.enation.app.javashop.framework.database.DaoSupport;
import com.enation.app.javashop.framework.exception.ServiceException;
import com.enation.app.javashop.framework.security.model.Seller;

/**
 * 店铺物流公司管理类
 * @author zhangjiping
 * @version v7.0.0
 * @since v7.0.0
 * 2018年3月29日 下午5:25:33
 */
@Service()
public class ShopLogiCompanyManagerImpl implements ShopLogiCompanyManager {
	
	@Autowired
	@Qualifier("memberDaoSupport")
	private DaoSupport daoSupport;
	@Autowired
	private LogiCompanyClient logiCompanyClient;
	

	@Override
	public List list() {
		Seller seller = UserContext.getSeller();
		String sql="SELECT count(*) from es_shop_logi_rel where logi_id = ? and shop_id = ? ";
		List<Map> lists = new ArrayList<>();
		List<LogiCompanyDO> list = logiCompanyClient.list();
		for (LogiCompanyDO logiCompanyDO : list) {
			Map map = ReflectionUtil.po2Map(logiCompanyDO);
			Integer integer = this.daoSupport.queryForInt(sql, logiCompanyDO.getId(), seller.getSellerId());
			if(integer!=0){
				map.put("shop_id",seller.getSellerId());
			}
			map.put("logi_id",logiCompanyDO.getId());
			lists.add(map);
		}
		return lists;
	}
	
	@Override
	@Transactional(value = "memberTransactionManager",propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	public void add(Integer logiId) {
		Seller seller = UserContext.getSeller();
		
		LogiCompanyDO model = logiCompanyClient.getModel(logiId);
		if(model==null) {
			throw new ServiceException(ShopErrorCode.E214.name(), "物流公司不存在");
		}
		
		int count = this.count(logiId, seller.getSellerId());
		if(count>0) {
			throw new ServiceException(ShopErrorCode.E215.name(), "物流公司已开启");
		}
		Map map=new HashMap(2);
		map.put("logi_id", logiId);
		map.put("shop_id", seller.getSellerId());
		this.daoSupport.insert("es_shop_logi_rel", map);
	}

	
	@Override
	@Transactional(value = "memberTransactionManager",propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	public void delete(Integer logiId) {
		Seller seller = UserContext.getSeller();
		LogiCompanyDO model = logiCompanyClient.getModel(logiId);
		int count = this.count(logiId, seller.getSellerId());
		if(count==0) {
			throw new ServiceException(ShopErrorCode.E216.name(), "物流公司已关闭");
		}
		String sql="delete from es_shop_logi_rel where shop_id=? and logi_id=?";
		this.daoSupport.execute(sql,seller.getSellerId(),logiId);
	}

	/**
	 * 查询指定物流公司
	 * @param logiId
	 * @param shopId
	 * @return
	 */
	 private Integer count(Integer logiId, Integer shopId) {
		String sql = "select count(*) from es_shop_logi_rel where shop_id = ? and logi_id = ? ";
		Integer count = this.daoSupport.queryForInt(sql, shopId , logiId);
		return count;
	}
	
}
